﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MapleLeaf.Client.Controls {

    public class InputBox : TextBox {

        public string Placehold {
            get => (string)GetValue(PlaceholdProperty);
            set => SetValue(PlaceholdProperty, value);
        }


        public InputBox() {
            VerticalContentAlignment = VerticalAlignment.Center;
        }


        public static MouseButtonEventHandler Button_MouseDown { get; }

        private static void OnButton_MouseDown(object sender, MouseButtonEventArgs e) {
            if (e.LeftButton == MouseButtonState.Pressed) {
                FrameworkElement frameworkElement = (FrameworkElement)e.Source;
                while (frameworkElement != null && !(frameworkElement.TemplatedParent is InputBox)) {
                    frameworkElement = (FrameworkElement)frameworkElement.Parent;
                }
                InputBox inputBox = (InputBox)frameworkElement.TemplatedParent;
                if (frameworkElement != null) {
                    inputBox.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Back) {
                        RoutedEvent = KeyDownEvent
                    });
                }
            }
        }


        static InputBox() {
            PlaceholdProperty = DependencyProperty.Register("Placehold", typeof(string), typeof(InputBox));
            Button_MouseDown = new MouseButtonEventHandler(OnButton_MouseDown);
        }


        public static readonly DependencyProperty PlaceholdProperty;
    }
}